from xiaoyiwy.xiaoyi.config import *
import requests,re

db = mysql()
img = {
    "openid": "371522",
    "barcode": "6928607402116"
}

def scan(img):
    data_json = {}
    openid = img['openid']
    barcode = img['barcode']
    url = "https://api.jisuapi.com/barcode2/query?appkey=2d4a7f2e12c6062f&barcode='%s'" % barcode
    rescode = {
        "201": "条码为空",
        "202": "条码不正确",
        "203": "该条码以下市",
        "204": "该条码已注册，但编码信息未按规定通报",
        "210": "没有信息"
    }
    conn = db.connection()
    cursor = conn.cursor()
    if openid and barcode:
        sql = "select drug_id from drug_barcode where barcode = '%s'" % barcode
        if cursor.execute(sql) > 0:
            # 取name查询本地药品表
            drug_id = cursor.fetchall()[0][0]
            sql1 = "select drug_barcode.drug_id drug_id,drug_instruct.* from drug_barcode " \
                   "join drug_instruct on drug_barcode.drug_id= drug_instruct.id " \
                   "where drug_barcode.drug_id = '%s'" % drug_id
            if cursor.execute(sql1) > 0:
                # 提取本地数据
                sqldata = cursor.fetchall()
                sqltitle = sql_index(cursor)
                resdata = []
                for sqlvalue in sqldata:  # 循环重构每一条sql返回数据字典
                    resdatai = {}
                    # index为字段列表的下标，value为字段列表的值
                    for index, value in enumerate(sqltitle):
                        # data[title]为{}的key，sqlvalue[index]为{}的value--{"data[title]":"sqlvalue[index]"}
                        resdatai[value] = sqlvalue[index]
                    resdata.append(resdatai)  # 把{"data[title]":"sqlvalue[index]"}添加到resdata = []列表
                drug_data = {
                    "code": 1,
                    "error": "",
                    "data": resdata
                }
                data_json = drug_data
            else:
                data_json = {"code": "0", "error": "该药品暂未收录","data": ""}
        else: # 本地无数据
            response_data = requests.get(url)
            code = response_data.json()['status']
            result_data = response_data.json()['result']
            res_name = result_data['name']
            res_company = result_data['company']
            res_barcode = result_data['barcode']
            res_ename = result_data['ename']
            if code == 0:
                sql2 = "select * from drug_instruct where currency_name = '%s' and company = '%s' LIMIT 1" % (res_name,res_company)
                if cursor.execute(sql2) > 0:
                    sqldata = cursor.fetchall()
                    sqltitle = sql_index(cursor)
                    resdata = []
                    for sqlvalue in sqldata:  # 循环重构每一条sql返回数据字典
                        resdatai = {}
                        # index为字段列表的下标，value为字段列表的值
                        for index, value in enumerate(sqltitle):
                            # data[title]为{}的key，sqlvalue[index]为{}的value--{"data[title]":"sqlvalue[index]"}
                            resdatai[value] = sqlvalue[index]
                        resdata.append(resdatai)  # 把{"data[title]":"sqlvalue[index]"}添加到resdata = []列表
                    data_json = {
                        "code": 1,
                        "error": "",
                        "data": resdata
                    }
                    try:
                        drug_id = resdata[0]['id']
                        drug_name = resdata[0]['currency_name']
                        sql3 = "select id from drug_barcode where drug_id = '%s'" % drug_id
                        if cursor.execute(sql3) > 0:
                            pass
                        else:
                            sql4 = "insert into drug_barcode (open_id,drug_id,drug_name,barcode) " \
                                   "value ('%s','%s','%s','%s')" % (openid,drug_id,drug_name,barcode)
                            try:
                                cursor.execute(sql4)
                                conn.commit()
                            except:
                                conn.rollback()
                    except:
                        pass
                    mysql_close(cursor, conn)
                else:
                    select_sql = "select id from drug_temporary where barcode = '%s'" % res_barcode
                    create_sql = "insert into drug_temporary (barcode,drug_name,ename,company) " \
                                 "value ('%s','%s','%s','%s')" % (res_barcode,res_name,res_ename,res_company)
                    if cursor.execute(select_sql) < 1:
                        try:
                            cursor.execute(create_sql)
                            conn.commit()
                        except:
                            conn.rollback()
                    data_json = { "code": "0", "error": "该药品暂未收录","data": "" }
            else:
                for key in rescode:
                    if code == key:
                        data_json = { "code": 0, "error": rescode[key],"data": "" }
    else:
        data_json = { "code": "0", "error": "openid与barcode为空","data": "" }
    print(data_json)
    return data_json

scan(img)


